faApp.controller('ReportBaseCtrl',['$scope','productService','reportService','$location','$controller','$route','$routeParams','$filter',
function($scope,productService,reportService,$location,$controller,$route,$routeParams,$filter) {
  $controller('BaseListCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;
  $scope.t = $routeParams.t;
  $scope.pCode = "";

  //产品解析报告
  $scope.report = {};
  $scope.resume = {};           //项目概要
  $scope.structureDiagram = []; //结构图
  $scope.repaymentSource = {};  //还款来源
  $scope.fundsUse = {};         //资金用途
  $scope.riskControl = [];      //风控措施
  $scope.lenders = [];          //融资方
  $scope.issuers = {};          //发行方
  $scope.relevantData = [];     //相关资料

  $scope.tabUrl = {};
  $scope.tabUrlMap = {
    xmgyUrl : "/report/xmgy?id=" + $scope.pid,
    jgtUrl  : "/report/jgt?id="  + $scope.pid,
    hklyUrl : "/report/hkly?id=" + $scope.pid,
    zjytUrl : "/report/zjyt?id=" + $scope.pid,
    fkcsUrl : "/report/fkcs?id=" + $scope.pid,
    rzfUrl  : "/report/rzf?id="  + $scope.pid,
    fxfUrl  : "/report/fxf?id="  + $scope.pid,
    xgzlUrl : "/report/xgzl?id=" + $scope.pid,
  };

  $scope.changeTab = function(tab){
    $scope.tabUrl[tab] = $scope.tabUrlMap[tab];
  };
  $scope.changeTab('xmgyUrl');

  //根据ID加载产品信息
  productService.productQuery($scope.pid).then(function(json){
    $scope.product = json.project;

    // 产品ID保存到产品分析中
    $scope.report["fpProductId"] = json.project.id;
    $scope.pCode = json.project.pCode;

    // 项目概要tab中显示融资方
    $scope.resume['productLendersName'] = $scope.resume['productLendersName'] || $scope.product.pProjectOwner;
  });

  $scope.reportPost = function(){
    if($scope.pid){

      reportService.deepAnalyticsDetail($scope.pid).then(function(json){
        if(!json.dto){
          alert("产品解析报告为空，请添加内容之后再发布！");
          return;
        }else if(!json.dto['fpProductResume']){
          alert("项目概要为空！");
          return;
        }else if(!json.dto['fpProductStructureDiagram']){
          alert("结构图为空！");
          return;
        }else if(!json.dto['fpProductRepaymentSource']){
          alert("还款来源为空！");
          return;
        }else if(!json.dto['fpProductRiskControl']){
          alert("风控措施为空！");
          return;
        }else if(!json.dto['fpProductLenders']){
          alert("交易对手为空！");
          return;
        }else if(!json.dto['fpProductFundsUse']){
          alert("资金用途为空！");
          return;
        }else if(!json.dto['fpProductIssuers']){
          alert("发行方为空！");
          return;
        }else if(!json.dto['fpProductRelevantData']){
          alert("相关资料为空！");
          return;
        }

        reportService.deepAnalyticsSave({"fpProductId":$scope.pid,"fpProductStatus":"1"}).then(function(json){
          if (json.statusCode){
            alert("出错了:" + json.statusCode);
          }else{
            alert("发布成功！");
          }
        });

      });

    }else{
      alert("产品ID为空，请刷新页面重试！");
    }
  }

  $scope.reportPostCancel = function(){
    if($scope.pid){
      reportService.deepAnalyticsSave({"fpProductId":$scope.pid,"fpProductStatus":"2"}).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          alert("取消发布成功！");
        }
      });
    }else{
      alert("产品ID为空，请刷新页面重试！");
    }
  }

  $scope.previewReport = function(){
    if($scope.pCode){
      //alert("111" + $scope.pCode);
      // window.open(App.staticHost + "/product/analyse3/" + $scope.pCode + "?preview=true");
      window.open(App.staticHost + "/analyse4/" + $scope.pCode + "?preview=true");
    }else{
      alert("页面出错，请刷新页面重试！");
    }
  }
}]);

faApp.controller('ReportXmgyCtrl',['$scope','productService','reportService','$location','$controller','$route','$routeParams','sessionService',function($scope,productService,reportService,$location,$controller,$route,$routeParams,sessionService) {
  $controller('ReportBaseCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;

  // 根据产品ID查询解析报告
  reportService.deepAnalyticsDetail($scope.pid).then(function(json){
    if(!json.dto){
      return;
    }
    
    $scope.report['id'] = json.dto['id'];

    if(json.dto['fpProductResume']){      
      $scope.resume = angular.fromJson(json.dto['fpProductResume'] || {});
    }
  });

  $scope.save = function(){
    //alert($scope.form.);
    //sessionService.setItem('reportResume',angular.toJson($scope.resume));
    $scope.report["fpProductResume"] = angular.toJson($scope.resume);
    reportService.deepAnalyticsSave($scope.report).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("保存成功！");
        $route.reload();
      }
    });
  }
}]);

faApp.controller('ReportJgtCtrl',['$scope','reportService','$location','$controller','$route','$routeParams','sessionService',function($scope,reportService,$location,$controller,$route,$routeParams,sessionService) {
  $controller('ReportBaseCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;
  $scope.structureDiagram = [];
  $scope.xsjt = "";
  $scope.xxjt = "";
  $scope.zydy = [];
  $scope.dbfs = [];
 
  $scope.structureDiagram.push({"xsjt": $scope.xsjt});
  $scope.structureDiagram.push({"xxjt": $scope.xxjt});

  reportService.deepAnalyticsDetail($scope.pid).then(function(json){
    if(!json.dto){
      return;
    }

    $scope.report['id'] = json.dto['id'];
    $scope.resume = angular.fromJson(json.dto['fpProductResume'] || {});

    if(json.dto['fpProductStructureDiagram']){
      
      $scope.structureDiagram = angular.fromJson(json.dto['fpProductStructureDiagram'] || []);

      $scope.zydy = $scope.structureDiagram[0].zydy;
      $scope.dbfs = $scope.structureDiagram[1].dbfs;

      $scope.xsjt = $scope.structureDiagram[2].xsjt;
      $scope.xxjt = $scope.structureDiagram[3].xxjt;
      // if(!$scope.structureDiagram){
      //   $scope.structureDiagram = [];
      //   $scope.riskControl.push({"zydy": zydy});
      //   $scope.riskControl.push({"dbfs": dbfs});
      // }
    }
  });

  $scope.addZydy = function(){
    if($scope.zydy.length < 3){
      $scope.zydy.push({"zydy":""});
    }else{
      alert("最多添加三条！");
    }
  };

  $scope.deleteZydy = function(i){
    $scope.zydy.splice(i, 1);
  };

  $scope.addDbfs = function(){
    if($scope.dbfs.length < 3){
      $scope.dbfs.push({"dbfs":""});
    }else{
      alert("最多添加三条！");
    }
  };

  $scope.deleteDbfs = function(i){
    $scope.dbfs.splice(i, 1);
  };

  $scope.save = function(){
    $scope.structureDiagram = [];

    $scope.structureDiagram.push({"zydy": $scope.zydy});
    $scope.structureDiagram.push({"dbfs": $scope.dbfs});
    $scope.structureDiagram.push({"xsjt": $scope.xsjt});
    $scope.structureDiagram.push({"xxjt": $scope.xxjt});

    $scope.report["fpProductStructureDiagram"] = angular.toJson($scope.structureDiagram);
    reportService.deepAnalyticsSave($scope.report).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("保存成功！");
        $route.reload();
      }
    });
  }
}]);

faApp.controller('ReportHklyCtrl',['$scope','reportService','$location','$controller','$route','$routeParams','sessionService',function($scope,reportService,$location,$controller,$route,$routeParams,sessionService) {
  $controller('ReportBaseCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;

  reportService.deepAnalyticsDetail($scope.pid).then(function(json){
    if(!json.dto){
      return;
    }

    $scope.report['id'] = json.dto['id'];

    if(json.dto['fpProductRepaymentSource']){
      $scope.repaymentSource = angular.fromJson(json.dto['fpProductRepaymentSource'] || {});
    }
  });

  $scope.save = function(){
    $scope.report["fpProductRepaymentSource"] = angular.toJson($scope.repaymentSource);
    // console.log($scope.repaymentSource);
    reportService.deepAnalyticsSave($scope.report).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("保存成功！");
        $route.reload();
      }
    });
  }
}]);

faApp.controller('ReportZjytCtrl',['$scope','reportService','$location','$controller','$route','$routeParams','sessionService',function($scope,reportService,$location,$controller,$route,$routeParams,sessionService) {
  $controller('ReportBaseCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;

  reportService.deepAnalyticsDetail($scope.pid).then(function(json){
    if(!json.dto){
      return;
    }

    $scope.report['id'] = json.dto['id'];

    if(json.dto['fpProductFundsUse']){
      $scope.fundsUse = angular.fromJson(json.dto['fpProductFundsUse'] || {});
    }
  });

  $scope.save = function(){
    $scope.report["fpProductFundsUse"] = angular.toJson($scope.fundsUse);
    reportService.deepAnalyticsSave($scope.report).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("保存成功！");
        $route.reload();
      }
    });
  }
}]);

faApp.controller('ReportFkcsCtrl',['$scope','reportService','$location','$controller','$route','$routeParams','sessionService',function($scope,reportService,$location,$controller,$route,$routeParams,sessionService) {
  $controller('ReportBaseCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;
  $scope.dbfsAll = [];
  $scope.dbfx = "";
  var kzbgTmp = [{"kzbg1":"","kzbg2":"","kzbg3":"","kzbg4":""},
                 {"kzbg1":"","kzbg2":"","kzbg3":"","kzbg4":""},
                 {"kzbg1":"","kzbg2":"","kzbg3":"","kzbg4":""},
                 {"kzbg1":"","kzbg2":"","kzbg3":"","kzbg4":""},
                 {"kzbg1":"","kzbg2":"","kzbg3":"","kzbg4":""},
                 {"kzbg1":"","kzbg2":"","kzbg3":"","kzbg4":""}];

  $scope.riskControl.push({"dbfx": ""});
  $scope.riskControl.push({"kzbg": kzbgTmp});

  $scope.kzbgAll = kzbgTmp;

  reportService.deepAnalyticsDetail($scope.pid).then(function(json){
    if(!json.dto){
      return;
    }

    $scope.report['id'] = json.dto['id'];

    if(json.dto['fpProductRiskControl']){
      $scope.riskControl = angular.fromJson(json.dto['fpProductRiskControl'] || []);

      $scope.dbfsAll = $scope.riskControl[0].dbfs;

      $scope.dbfx = $scope.riskControl[1]['dbfx'] || "";
      $scope.kzbgAll = $scope.riskControl[2]['kzbg'].length > 0 ? $scope.riskControl[2]['kzbg'] : kzbgTmp;
    }

    // console.log($scope.riskControl[1]['kzbg']);
  });

  $scope.addDbfs = function(){
    if($scope.dbfsAll.length < 8){
      $scope.dbfsAll.push({"bt":"","ms":""});
    }else{
      alert("最多添加八条！")
    }
  };

  $scope.deleteDbfs = function(i){
    $scope.dbfsAll.splice(i, 1);
  };

  $scope.save = function(){
    var isShow = true;
    $scope.riskControl = [];

    $scope.riskControl.push({"dbfs":$scope.dbfsAll});
    $scope.riskControl.push({"dbfx":$scope.dbfx});

    for (var i = 0; i < $scope.kzbgAll.length; i++) {
      if($scope.kzbgAll[i]['kzbg1'] || $scope.kzbgAll[i]['kzbg2'] ||
         $scope.kzbgAll[i]['kzbg3'] || $scope.kzbgAll[i]['kzbg4']){
        isShow = false;
        break;
      }
    };

    $scope.riskControl.push({"kzbg": isShow ? [] : $scope.kzbgAll});

    $scope.report["fpProductRiskControl"] = angular.toJson($scope.riskControl);
    reportService.deepAnalyticsSave($scope.report).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("保存成功！");
        $route.reload();
      }
    });
  }
}]);

faApp.controller('ReportRzfCtrl',['$scope','reportService','$location','$controller','$route','$routeParams','sessionService',function($scope,reportService,$location,$controller,$route,$routeParams,sessionService) {
  $controller('ReportBaseCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;
  reportService.deepAnalyticsDetail($scope.pid).then(function(json){
    if(!json.dto){
      return;
    }

    $scope.report['id'] = json.dto['id'];

    if(json.dto['fpProductLenders']){

      $scope.lenders = angular.fromJson(json.dto['fpProductLenders'] || []);
      for(i in $scope.lenders){
        $scope.lenders[i].shareHolder = $scope.lenders[i].shareHolder || [{gdxx: '',gdlx: '',gd: '',zjlx: '',zjhm: ''}];
      };
      console.log($scope.lenders);
    }
  });
  $scope.addLenders = function(){
    $scope.lenders.push({"zch":"","gsmc":"","lx":"","fddb":"","zs":"","zczb":"","clrq":"","jyqxf":"","jyqxt":"","jjfw":"","fjjg":"","hzrq":"","djzt":"","shareHolder":[{'gdxx' : '','gdlx' : '','gd': '','zjlx' : '','zjhm' : ''}]});
  };
  var shareHolderIndex = 1;
  $scope.addShareHolder = function(i){
    $scope.lenders[i].shareHolder.push({gdxx : '',gdlx : '',gd : '',zjlx : '',zjhm : ''});
  }
  $scope.deleteShareHolder = function(p,i){
    $scope.lenders[p].shareHolder.splice(i, 1);
    $scope.save();
  }
  $scope.deleteLenders = function(i){
    $scope.lenders.splice(i, 1);
  };

  $scope.save = function(){
    $scope.report["fpProductLenders"] = angular.toJson($scope.lenders);
    reportService.deepAnalyticsSave($scope.report).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("保存成功！");
        $route.reload();
      }
    });
  }
}]);

faApp.controller('ReportFxfCtrl',['$scope','reportService','$location','$controller','$route','$routeParams','sessionService',function($scope,reportService,$location,$controller,$route,$routeParams,sessionService) {
  $controller('ReportBaseCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;

  reportService.deepAnalyticsDetail($scope.pid).then(function(json){
    if(!json.dto){
      return;
    }

    $scope.report['id'] = json.dto['id'];

    if(json.dto['fpProductIssuers']){
      $scope.issuers = angular.fromJson(json.dto['fpProductIssuers'] || {});
    }
  });

  $scope.save = function(){
    $scope.report["fpProductIssuers"] = angular.toJson($scope.issuers);
    reportService.deepAnalyticsSave($scope.report).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("保存成功！");
        $route.reload();
      }
    });
  }
}]);

faApp.controller('ReportXgzlCtrl',['$scope','reportService','$location','$controller','$route','$routeParams','sessionService',function($scope,reportService,$location,$controller,$route,$routeParams,sessionService) {
  $controller('ReportBaseCtrl', {$scope: $scope});
  $scope.pid = $routeParams.id;

  reportService.deepAnalyticsDetail($scope.pid).then(function(json){
    if(!json.dto){
      return;
    }

    $scope.report['id'] = json.dto['id'];

    if(json.dto['fpProductRelevantData']){
      $scope.relevantData = angular.fromJson(json.dto['fpProductRelevantData'] || []);
    }
  });

  $scope.addRelevantData = function(){
    if($scope.relevantData.length >= 10){
      alert("资讯最多十条！");
      return;
    }
    $scope.relevantData.push({"zxbt":"","zxdz":""});
  };

  $scope.deleteRelevantData = function(i){
    if($scope.relevantData.length <= 2){
      alert("资讯最低两条！");
      return;
    }
    $scope.relevantData.splice(i, 1);
  };

  $scope.textChange = function(o){
    var t = $("#" + o).val();
    if(t != undefined && t.length > 28){
      $("#" + o).val(t.substr(0,28));
      alert("资讯标题最多28个字，已自动截取！");
    }
  }

  $scope.save = function(){
    $scope.report["fpProductRelevantData"] = angular.toJson($scope.relevantData);
    reportService.deepAnalyticsSave($scope.report).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("保存成功！");
        $route.reload();
      }
    });
  }
}]);